package com.drawu.drawuspring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.drawu.drawuspring.domain.po.Participants;
import com.drawu.drawuspring.mapper.ParticipantsMapper;
import com.drawu.drawuspring.service.IParticipantsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drawu.drawuspring.utils.AppThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 活动参与者记录表[5](@ref) 服务实现类
 * </p>
 *
 * @author hui
 * @since 2025-04-01
 */
@Service
public class ParticipantsServiceImpl extends ServiceImpl<ParticipantsMapper, Participants> implements IParticipantsService {

    @Override
    public void removeByActId(Long id) {
        remove(Wrappers.<Participants>lambdaQuery().eq(Participants::getActivityId, id));
    }

    @Override
    public List<Participants> queryByActId(Long id) {
        return  list(Wrappers.<Participants>lambdaQuery().eq(Participants::getActivityId, id).orderByDesc(Participants::getJoinTime));
    }

    @Override
    public void savePat(List<Integer> userIds, Integer id) {
        for (Integer userId : userIds) {
            Participants participants = new Participants();
            participants.setActivityId(id);
            participants.setUserId(userId);
            save(participants);
        }
    }

    @Override
    public List<Integer> queryByActId() {
        Integer id = AppThreadLocalUtil.getUser().getId();
        QueryWrapper<Participants> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        List<Participants> list = list(queryWrapper);
        return list.stream().map(Participants::getActivityId).toList();
    }

    @Override
    public boolean removeByActIdAndUserId(Long activityId, Integer userId) {
        Participants participants = getOne(Wrappers.<Participants>lambdaQuery().eq(Participants::getActivityId, activityId).eq(Participants::getUserId, userId));
        if (participants == null) {
            return false;
        }
        removeById(participants.getId());
        return true;
    }
}
